import { TBY } from '@/api/config/servicePort'
import http from '@/api'
import { ResPage, ReqPage } from '@/api/interface'
import { CascaderOption } from 'element-plus'

export namespace TbyCategoryDict {
  export interface Query extends ReqPage {
    category?: string
  }

  export interface Record {
    id: number
    category: string
    code: string
    name: string
    parentId: number
    children: Record[]
  }

  export interface Form {
    id: number
    category: string
    code: string
    name: string
    parentId: number
    categoryLabel: string
  }

  /**类型枚举 */
  export const Category = [{ value: 'activity_category', label: '科研调查活动总类' }]
}

// 树型表格数据
export function treeTableList(params: TbyCategoryDict.Query) {
  return http.get<ResPage<TbyCategoryDict.Record>>(TBY + '/category_dict/tree_table_list', params)
}

// 下拉选
export function optionsCategoryDict(params: TbyCategoryDict.Query) {
  return http.get<OptionType[]>(TBY + '/category_dict/options', params)
}

export function cascaderOptionCategoryDict(params: TbyCategoryDict.Query) {
  return http.get<CascaderOption[]>(TBY + '/category_dict/options', params)
}

// 详情
export const getCategoryDictDetail = (id: number) => {
  return http.get<Partial<TbyCategoryDict.Form>>(TBY + `/category_dict/get`, { id }, { loading: false })
}

// 新增或修改详情
export const saveOrUpdateCategoryDict = (params: Partial<TbyCategoryDict.Form>) => {
  return http.post(TBY + `/category_dict/save`, params)
}

// 删除分类
export const removeCategoryDict = (id: number) => {
  return http.delete(TBY + `/category_dict/delete`, { id })
}

/**获取分类文字 */
export const getCategoryLabel = (category?: string) => {
  if (!category) {
    return ''
  }
  let value: string = ''
  for (let i = 0; i < TbyCategoryDict.Category.length; i++) {
    if (TbyCategoryDict.Category[i].value === category) {
      value = TbyCategoryDict.Category[i].label
      break
    }
  }
  return value
}

// 验证设备编码是否存在
export const codeExist = (code: string, id?: number) => {
  return http.get<boolean>(TBY + '/category_dict/exist', { code, id }, { loading: false })
}
